// miniprogram/pages/main/buy/index.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    deliveryMode: "express",
    items: [],
    name: "",
    phone: "",
    selectAddress: {},
    mealFee: "0",
    showView:false

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.postRequestLoading("/rest/dishorder/view.htm", options, this.backViewData);

  },
  backViewData(res) {
    if (res.code == 0) {
      this.setData({
        model: res,
        showView:true
      });
    }
  },
  init() {
    app.postRequest("/rest/customeraddress/viewDefault.htm", {}, this.backViewDefaultOrder);

  },
  backViewDefaultOrder(res) {
    if (res.code == 0) {
      this.setData({
        selectAddress: res
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  selectAddress: function () {
    wx.navigateTo({
      url: '/pages/my/my_address/home/index?type=select',
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  goShop: function () {
    wx.openLocation({
      latitude: app.selectShop.lat,
      longitude: app.selectShop.lng,
      name: app.selectShop.name,
      address: app.selectShop.address,
      scale: 17
    })
  },

  /**
   * 用户点击右上角分享
   */
  buy: function () {

    var param = {};
    var items = app.cartItems;
    var tempItems = items.map(item => {
      var result = {};
      result.dish = item.dishId;
      result.dishSku = item.skuId;
      result.amount = item.num;
      return result;
    });
    param.items = tempItems;
    param.deliveryMode = this.data.deliveryMode;
    param.note = this.data.note;
    if (this.data.deliveryMode == "self") {
      if (app.isEmpty(this.data.name)) {
        app.showModal("姓名不能为空");
        return
      }
      if (app.isEmpty(this.data.phone)) {
        app.showModal("电话不能为空");
        return
      }
    } else {
      param.customerAddress = this.data.selectAddress.id;

    }
    param.consignee = this.data.name;
    param.phone = this.data.phone;
    param.shop = app.shop;
    app.postJson("/rest/dishorder/order.htm", param, this.backOrder);
  },
  backOrder(res) {
    if (res.code == 0) {
      app.pay(res.pay, this.paySuccess, this.payFail);
    } else {
      app.showModal(res.msg);
    }
  },
  paySuccess() {
    var model = this.data.model;
    wx.redirectTo({
      url: '/pages/my/my_order_view/index?id=' + model.id,
    })
  },
  payFail() {
    var model = this.data.model;
    var param = {};
    param.id = model.id;
    app.postRequest("/rest/dishorder/cancelPay.htm", param, res=> {});

  },
  callTel() {
    var model = this.data.model;
    if (model.shopTel) {
      wx.makePhoneCall({
        phoneNumber: model.shopTel,
      })
    }
  },
  cancelPay() {
    var model = this.data.model;
    var param = {};
    param.id = model.id;
    app.postRequest("/rest/dishorder/cancelOrder.htm", param, this.backCancelPay);
  },
  refundOrder(){
    var model = this.data.model;
    var param = {};
    param.id = model.id;
    app.postRequest("/rest/dishorder/refundOrder.htm", param, this.backCancelPay);
  },
  backCancelPay(res) {
    if (res.code == 0) {
      var model = this.data.model;
      wx.redirectTo({
        url: '/pages/my/my_order_view/index?id=' + model.id,
      })
    }
  },
  pay() {
    var model = this.data.model;
    var param = {};
    param.id = model.id;
    app.postRequestLoading("/rest/dishorder/rePay.htm", param, this.backOrder);
  },
  goShop(){
    var model = this.data.model;
    
    wx.redirectTo({
      url: '/pages/main/shop/index?id='+model.shop
    })
  }
})